package com.dati.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import util.TotalClass;
import util.UtilDAO;

import com.dati.bean.DatiType;
import com.dati.bean.Xulie;

public class DatiTypeDao {

	/**
	 *@author qingyu zhang
	 *@function:
	 *2011-4-4
	 *下午02:41:48
	 *zzClub
	 *com.dati.dao
	 */
	private ResultSet rs = null;
	// 成功失败标志
	private boolean flag = false;
	private Statement st = null;
	// 要执行的语句
	private String sql = "";
	private List<DatiType> datiTypeList = null;
	private List<Xulie> xulieList = null;
	/**
	 * 
	 *@author qingyu zhang
	 *@function:
	 * @param datiType
	 * @return
	 *2011-4-6
	 *下午04:31:46
	 *zzClub
	 *com.dati.dao
	 *boolean
	 */
	public boolean addDatiType(DatiType datiType){
		boolean bo = false;
		sql = "insert into tb_answer_type (answerTypeId,userId,typeName,createTime)" +
				" values('"+datiType.getAnswerTypeId()+"',"+datiType.getUserId()+",'"+datiType.getTypeName()+"','"+datiType.getCreateTime()+"')";
		bo = new TotalClass().operatorObject(sql);
		return bo;
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:根据条件得到符合条件的答题类型
	 * @param table
	 * @param order
	 * @param condition
	 * @return
	 *2011-4-7
	 *上午09:10:54
	 *zzClub
	 *com.dati.dao
	 *List<DatiType>
	 */
	public List<DatiType> getAllDaTiType(String table,String order,String condition){
		sql=UtilDAO.getList(table, order, condition);
		System.out.println("语句＝＝"+sql);
		rs = new TotalClass().getResult(sql);
		try {
			while(rs.next()){
				if(datiTypeList==null){
					datiTypeList= new ArrayList<DatiType>();
				}
				datiTypeList.add(this.getDatiType(rs));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			return datiTypeList;
		}
	}
	public DatiType getDatiType(ResultSet rs){
		DatiType dt = new DatiType();
		try {
			dt.setAnswerTypeId(rs.getString("answerTypeId"));
			dt.setCreateTime(rs.getString("createTime"));
			dt.setTypeName(rs.getString("typeName"));
			dt.setUserId(rs.getInt("userId"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dt;
		
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:
	 * @param typeId
	 * @return
	 *2011-4-7
	 *下午02:02:54
	 *zzClub
	 *com.dati.dao
	 *DatiType
	 */
	public DatiType getDatiType(String typeId){
		DatiType datiType = null;
		sql="select * from tb_answer_type where answerTypeId='"+typeId+"'";
		rs= new TotalClass().getResult(sql);
		try {
			while(rs.next()){
				if(datiType==null){
					datiType = new DatiType();
				}
				datiType = this.getDatiType(rs);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return datiType;
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:根据答题的类型来取得此答题类型下的系列
	 * @param table
	 * @param order
	 * @param condition
	 * @return
	 *2011-4-7
	 *上午09:20:34
	 *zzClub
	 *com.dati.dao
	 *List<Xulie>
	 */
	public List<Xulie> getXulieByTypeId(String table,String order,String condition){
		sql=UtilDAO.getList(table, order, condition);
		System.out.println("系列语句="+sql);
		rs = new TotalClass().getResult(sql);
		try {
			while(rs.next()){
				if(xulieList==null){
					xulieList = new ArrayList<Xulie>();
				}
				xulieList.add(this.getXulie(rs));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return xulieList;
	}
	public Xulie getXulie(ResultSet rs){
		Xulie xl = new Xulie();
		try {
			xl.setAnswerTypeId(rs.getString("answerTypeId"));
			xl.setCreateTime(rs.getString("createTime"));
			xl.setSeriseName(rs.getString("seriseName"));
			xl.setSeriseId(rs.getString("seriseId"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return xl;
	}
	public boolean modifyDatiType(DatiType dt){
		boolean bo = false;
		sql = "update tb_answer_type set typeName='"+dt.getTypeName()+"',createTime='"+dt.getCreateTime()+"' where answerTypeId='"+dt.getAnswerTypeId()+"'";
		System.out.println("sql=="+sql);
		bo = new TotalClass().operatorObject(sql);
		return bo ;
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:新增系列
	 * @param xulie
	 * @return
	 *2011-4-7
	 *下午03:52:54
	 *zzClub
	 *com.dati.dao
	 *boolean
	 */
	public boolean addXulie(Xulie xulie){
		boolean bo = false;
		sql="insert into tb_serise (seriseId,answerTypeId,seriseName,createTime) " +
				"values('"+xulie.getSeriseId()+"','"+xulie.getAnswerTypeId()+"','"+xulie.getSeriseName()+"','"+xulie.getCreateTime()+"')";
		bo = new TotalClass().operatorObject(sql);
		return bo ;
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:根据系列的ＩＤ得到此系列对象
	 * @param xilie
	 * @return
	 *2011-4-8
	 *上午07:47:54
	 *zzClub
	 *com.dati.dao
	 *Xulie
	 */
	public Xulie getXulie(String xilie){
		Xulie xl = new Xulie();
		sql = "select * from  tb_serise where seriseId='"+xilie+"'";
		System.out.println("修改系列="+sql);
		rs = new TotalClass().getResult(sql);
		try {
			if(rs.next()){
				xl = this.getXulie(rs);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return xl;
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:修改系列名称
	 * @param xl
	 * @return
	 *2011-4-8
	 *上午08:09:48
	 *zzClub
	 *com.dati.dao
	 *boolean
	 */
	public boolean modifyXulie(Xulie xl){
		boolean bo = false;
		sql = "update tb_serise set seriseName='"+xl.getSeriseName()+"' ,createTime='"+xl.getCreateTime()+"' where seriseId='"+xl.getSeriseId()+"'";
		bo = new TotalClass().operatorObject(sql);
		return bo ;
	}
	/**
	 * 把系列名称存放到一个LIST中
	 *@author qingyu zhang
	 *@function:
	 * @param xilie
	 * @return
	 *2011-4-18
	 *下午03:12:42
	 *zzClub
	 *com.dati.dao
	 *List<String>
	 */
	public List<String> getXilieNameList(List<Xulie> xilie){
		List<String> list = new ArrayList<String>();
		if(xilie!=null&&xilie.size()>0){
			for(Xulie xl:xilie){
				list.add(xl.getSeriseName());
			}
		}
		return list;
	}
	/**
	 * 批量导入答题类型
	 *@author qingyu zhang
	 *@function:
	 * @param datiTypeList
	 * @return
	 *2011-4-18
	 *下午09:52:22
	 *zzClub
	 *com.dati.dao
	 *boolean
	 */
	public boolean saveDatiType(List<DatiType> datiTypeList){
		boolean bo = false;
		st = new TotalClass().getStatement();
		try{
			for(DatiType datiType:datiTypeList){
				String hql = "insert into tb_answer_type (answerTypeId,userId,typeName,createTime) " +
						"values('"+datiType.getAnswerTypeId()+"',"+datiType.getUserId()+",'"+datiType.getTypeName()+"','"+datiType.getCreateTime()+"')";
				System.out.println("类型语句"+hql);
				st.addBatch(hql);
			}
			st.executeBatch();
			bo = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return bo;
	}
	/**
	 * 批量导入系列
	 *@author qingyu zhang
	 *@function:
	 * @param xilieList
	 * @return
	 *2011-4-18
	 *下午09:57:07
	 *zzClub
	 *com.dati.dao
	 *boolean
	 */
	public boolean saveXilie(List<Xulie> xilieList){
		boolean bo = false;
		st = new TotalClass().getStatement();
		try{
			for(Xulie xilie:xilieList){
				String hql = "insert into tb_serise (seriseId,answerTypeId,seriseName,createTime) " +
						"values('"+xilie.getSeriseId()+"','"+xilie.getAnswerTypeId()+"','"+xilie.getSeriseName()+"','"+xilie.getCreateTime()+"')";
				System.out.println("系列"+hql);
				st.addBatch(hql);
			}
			
			st.executeBatch();
			bo = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return bo;
	}
}
